﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Web.UI;
using NickLee.Common;


namespace GPCSC.Web.SYS_ORG
{
    public partial class dwUserEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.command.Value = Usual.f_querystringToString("command");
                byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];

                iden.Value = users.idenID.ToString();

                if (this.command.Value == "View")
                {
                    string postid = Usual.f_querystringToString("CommandArg");
                    f_fristLoad(postid);
                    UIFactory.UIFactory_ReadOnly(this);
                    this.buttontr.Style.Add("display", "none");
                }
                else if (this.command.Value == "Edit")
                {
                    string postid = Usual.f_querystringToString("CommandArg");
                    UIFactory.UIFactory_NotReadOnly(this);
                    f_fristLoad(postid);
                }
                else if (this.command.Value =="New")
                { 
                       
                        this.MtxtD_CL_POSTID_PK.Text = users.CL_POSTID_PK.ToString();
                        this.MtxtD_CL_ROLEOID_PK.Text = "1224a88c-59fb-42c0-ae2e-b5a2f623b653";
                }

            }
        }

        protected void f_fristLoad(string PrimaryKey)
        {
            base.o_uiTable.Clear();
            SqlReadXml org = new SqlReadXml();
            base.o_uiTable.Add("@USERID", PrimaryKey);
            base.m_uiDataSet = org.ReaderDataSet("sel_user_one", base.o_uiTable);
            if (base.m_uiDataSet.Tables.Count > 0)
            {
                if (base.m_uiDataSet.Tables[0].Rows.Count > 0)
                {
                    //UIFactory set UI controls value:界面工厂设置界面控件值
                    try
                    {
                        base.o_uiTable = UIFactory.UIFactory_Set(this, base.m_uiDataSet.Tables[0]);

                    }
                    catch
                    {

                    }
                    //your can do databind yourself:对界面工厂不能填充的数据，通过ODRM手动填充
                    this.TEL.Value = base.m_uiDataSet.Tables[0].Rows[0]["TEL"].ToString();
                    GPCSC.Model.CL_POST_S objModel = new GPCSC.Model.CL_POST_S();
                    var row = base.m_uiDataSet.Tables[0].Rows[0];
                    ODRM.DataRowConvertObject(row, objModel);
                    this.sdg.Value = Usual.f_convertString(row["DepartName"]);
                    this.ddlRegType.Value = Usual.f_convertString(row["PD_CompanyName"]);

                }
            }
        }
        public Hashtable f_getElements(Hashtable o_uiTable)
        {
            //UIFactory Get Values as Hashtable:界面工厂获取值，并保存在哈希表
            base.o_uiTable.Clear();
            base.o_uiTable = UIFactory.UIFactory_Get(this);
            string relname = this.MtxtD_REALNAME.Value;
            if (relname == "")
            {
                relname = this.MtxtD_USERNAME.Value;
                o_uiTable.Remove("REALNAME");
                o_uiTable.Add("REALNAME", relname);
            }
            if (Usual.f_querystringToObj("CommandArg") != null)
            {
                base.o_uiTable.Add("CL_USER_SOID", Usual.f_convertGuid(Usual.f_querystringToObj("CommandArg")));

               // base.o_uiTable.Add("DepartName", this.MtxtD_DepartName.Text);
            }
            else
            {
                base.o_uiTable.Add("CL_USER_SOID", Guid.NewGuid());
               // base.o_uiTable.Add("DepartName", this.MtxtD_DepartName.Text);
            }
            base.o_uiTable.Add("TEL",this.TEL.Value);
            return base.o_uiTable;
        }
  
        



        protected void Button1_Click(object sender, EventArgs e)
        {
            //if (MtxtD_CL_ROLEOID_PK.Text == "")
            //{
            //    MtxtD_CL_ROLEOID_PK.Text = "00000000-0000-0000-0000-000000000000";
            //}
            //if (MtxtD_CL_POSTID_PK.Text == "")
            //{
            //    MtxtD_CL_POSTID_PK.Text = "00000000-0000-0000-0000-000000000000";
            //}
            savepost();
        }
        protected void savepost()
        {
            try
            {
                Hashtable htable = f_getElements(base.o_uiTable);

                SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
                if (this.command.Value == "New")
                {
                    dtable.databyhtables(htable, "add", "CL_USER_S", "", "");
                    this.Response.Write("<script>alert('添加人员成功！');window.location.href='dwUserList.aspx';</script>");
                }
                else if (this.command.Value == "Edit")
                {
                    dtable.databyhtables(htable, "update", "CL_USER_S", "CL_USER_SOID", Usual.f_querystringToObj("CommandArg").ToString());
                    this.Response.Write("<script>alert('更新人员成功！');window.location.href='dwUserList.aspx';</script>");
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }


        }
    }
}